import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    //   公共的数据仓库
    state: {
        count: 0,
        list: [],
        shopAllList: [],
        shopCarList: []
    },
    mutations: {
        addCount(state) {
            state.count++
        },
        // store传参
        setList(state, data) {
            console.log("data", data);
            state.list.push(data);
        },


        //ShopData添加进去的商品
        addShopList(state, ShopData) {
            console.log("数据仓库", ShopData);
            state.shopAllList.push(ShopData)
        },

        //增加购物车列表数据
        addShopCarList(state, shopData) {
            state.shopCarList.push(shopData);
        },

        //通过下标拿到商品的数量,并且自增
        addCount(state, shopId) {
            let index = state.shopCarList.findIndex(item => {
                return item.id === shopId;
            })
            state.shopCarList[index].count++;
        },
        //通过下标减少商品的数量
        deleteCount(state, shopId) {
            let index = state.shopCarList.findIndex(item => {
                return item.id === shopId;
            });
            if (state.shopCarList[index].count === 1) {
                return
            } 
                state.shopCarList[index].count--;
            
        },
        deleteGoods(state,shopId){
            let index =state.shopCarList.findIndex(item=>{
                return item.id===shopId;
            })
            state.shopCarList.splice(index,1)
        }
    }
})

export default store
